<script setup>
import { ref, reactive, onMounted } from 'vue'
import { config, list, getIndex, trendtop } from '@/api/index.js'
import CardIndex from '@/components/Card/cardIndex.vue'

const subListState = ref(false)
const subListIndex = ref(0)
const navData = ref({})
const refreshing = ref(false)
const state = reactive({
  cards: []
})
const preload = ref(false)
const loading = ref(false)
const finished = ref(false)
const since_id = ref(0)
const page = ref(0)
let tabItem = reactive({})

preload.value = true

const onRefresh = () => {
  setTimeout(() => {
    getIndexData()
    refreshing.value = false;
  }, 1000)
}

const onLoad = () => {
  loading.value = true
  loadList(tabItem)
}

const getConfig = () => {
  config().then(res => {
    if (!document.cookie.includes('XSRF-TOKE')) {
      document.cookie = `XSRF-TOKEN=${res.data.st}`
    }
  })
}

const getList = async () => {
  const res = await list()
  navData.value = res.data
}

const clickSubItem = (item, idx) => {
  window.scrollTo(0, 0)
  preload.value = true
  subListIndex.value = idx
  subListState.value = false
  tabItem = item
  since_id.value = 0
  page.value = 0
  loadList(tabItem)
}

const getIndexData = async () => {
  const params = {
    containerid: 102803,
    openApp: 0,
    since_id: since_id.value >= 1 ? since_id.value : null
  }
  const res = await getIndex({ ...params })
  since_id.value = res.data.cardlistInfo.since_id
  state.cards.push(...res.data.cards)
  preload.value = false
  loading.value = false
}

const getContainerid = async (containerid) => {
  console.log(containerid)
  const params = {
    containerid,
    page: page.value >= 1 ? page.value : null
  }
  const res = await trendtop({ ...params })
  page.value = res.data.page
  const mb = res.data.statuses.map(item => {
    item.mblog = { ...item }
    item.mblog.user = item.user
    return item
  })
  state.cards.push(...mb)
  preload.value = false
  loading.value = false
}

const loadList = (item) => {
  if (item.name === '热门') {
    getIndexData()
  } else {
    getContainerid(item.gid)
  }
}

onMounted(() => {
  getConfig()
  getList()
  getIndexData()
})

</script>

<template>
  <div class="main-wrap">
    <div class="lite-topbar main-top">
      <div class="nav-top">
        <div v-if="false" class="nav-left lite-iconf">
          <van-icon name="contact-o" />
        </div>
        <div class="nav-left unlogin-logo"></div>
        <a class="nav-search unlogin-search">
          <aside>
            <label class="m-search">
              <div class="m-font m-font-search">
                <van-icon name="search" />
              </div>
              <div class="m-text-cut">大家都在搜：{{ navData.hot?.hotWord }}</div>
            </label>
          </aside>
        </a>
        <div class="nav-right">
          <div class="lite-iconf lite-iconf-releas">
            <van-icon name="edit" />
          </div>
        </div>
      </div>
      <div class="nav-main">
        <div class="m-box">
          <div class="m-box-col inner-box">
            <div class="scroll-box slide-container">
              <div class="slide-wrap">
                <ul class="nav_item">
                  <li class="item_li" :class="{ cur: idx === subListIndex }" @click="clickSubItem(item, idx)"
                    v-for="(item, idx) in navData.channel" :key="item.gid">
                    <span>
                      {{ item.name }}
                      <em></em>
                    </span>
                  </li>
                </ul>
              </div>
              <div class="lite-nav-sublist m-col-4" v-if="subListState">
                <ul class="m-auto-list">
                  <li class="m-auto-box" :class="{ cur: idx == subListIndex }" @click="clickSubItem(item, idx)"
                    v-for="(item, idx) in navData.channel" :key="item.gid">
                    <span class="m-text-cut">{{ item.name }}</span>
                  </li>
                </ul>
              </div>
            </div>
          </div>
          <div @click="subListState = !subListState" class="nav-plus m-box-center m-box-center-a">
            <van-icon name="arrow-down" :class="{ 'aniRotate': subListState }" class="m-font-arrow-down" />
          </div>
        </div>
      </div>
    </div>
    <div v-if="preload" class="pannelwrap">
      <div class="pannel" v-for="iten in 4" :key="iten">
        <van-skeleton title avatar :row="5" />
        <footer class="f-footer-ctrl">
          <div class="m-diy-btn m-box-center-a">
            <i class="lite-iconf lite-iconf-report">
              <van-icon name="share-o" />
            </i>
          </div>
          <div class="m-diy-btn m-box-center-a">
            <i class="lite-iconf lite-iconf-report">
              <van-icon name="chat-o" />
            </i>
          </div>
          <div class="m-diy-btn m-box-center-a">
            <i class="lite-iconf lite-iconf-report">
              <van-icon name="good-job-o" />
            </i>
          </div>
          <aside>
            <van-icon class="lite-iconf m-font-dot-more" name="ellipsis" />
          </aside>
        </footer>
      </div>
    </div>
    <van-pull-refresh v-if="!preload" v-model="refreshing" @refresh="onRefresh" pulling-text="↓ 下拉刷新"
      loosing-text="↑ 释放更新">
      <van-list v-model:loading="loading" :immediate-check="false" :finished="finished" finished-text="没有更多了"
        @load="onLoad">
        <div class="pannelwrap">
          <div class="wb-item-wrap" v-if="tabItem.name === '同城'">
            <div class="card card11">
              <div>
                <div class="card-list">
                  <div class="card m-panel card6">
                    <div class="card-wrap">
                      <div class="card-main">
                        <a>遇到 “私信骚扰、低俗评论” 怎么办?</a>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <CardIndex v-for="(card, index) in state.cards" :key="index" :card="card" pageType="index" />
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<style scoped lang="scss">
.f-footer-ctrl {
  border-top-width: 0;
  height: 17px;
  padding: 16px 6px 16px 0;
  margin: 0 12px;
  overflow: hidden;

  .m-diy-btn {
    color: rgba(40, 47, 60, 0.8);
    height: 100%;
    float: left;

    h4 {
      font-size: 13px;
      display: inline-block;
      margin-top: 0;
      margin-left: 4px;
    }

    &+.m-diy-btn {
      margin-left: 27px;
    }
  }

  .lite-iconf {
    font-size: 18px;
  }

  aside {
    float: right;
    color: rgba(40, 47, 60, 0.8);
  }

  .m-font-dot-more {
    display: flex;
  }
}

.pannel {
  padding-top: 20px;
  background: #fff;
  overflow: hidden;
}

.main-wrap {
  margin: 84px 0 28px 0;
}

.lite-topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: 0;
  background-color: #fafafa;
  color: #262626;
  border-top-width: 0;
  border-right-width: 0;
  border-bottom-width: 1px;
  border-left-width: 0;
  border-color: #e6e6e6;
  border-style: solid;
  font-size: 16px;
  z-index: 995;

}

.lite-topbar {

  .nav-left,
  .nav-search,
  .nav-main,
  .nav-right {
    position: absolute;
    line-height: 44px;
  }

  .main-top {
    height: 84px;
  }

  .nav-left {
    left: 12px;
  }

  .nav-search {
    left: 44px;
    right: 86px;
    line-height: 40px;

    &.unlogin-search {
      right: 44px;
    }
  }

  .nav-right {
    right: 15px;

    .lite-iconf-releas {
      right: 0;
    }

    .lite-iconf {
      position: absolute;
      color: #282f3c;
    }
  }

  &.main-top {
    height: 84px;

    .nav-main {
      top: 40px;
      right: 0;
      left: 0;
      font-size: 1rem;
      color: #a5adb5;

      .inner-box {
        overflow: hidden;
        width: 1px;

        &:after,
        &:before {
          content: "";
          position: absolute;
          top: 0;
          bottom: 3px;
          z-index: 9;
          width: 12px;
        }

        &:before {
          left: 0;
          background: linear-gradient(90deg, #fafafa 0, #fafafa 50%, hsla(0, 0%, 98%, 0));
        }
      }

      .scroll-box {
        scroll-behavior: smooth;
      }
    }
  }

  .nav-main {
    .nav_item {
      white-space: nowrap;
      font-size: 0;

      .item_li {
        display: inline-block;
        margin: 0 12px;

        &.cur {
          color: #000;

          span {
            em {
              display: block;
              position: absolute;
              bottom: -5px;
              left: 50%;
              width: 14px;
              margin-left: -5.4px;
              border-top: 2px solid #282f3c;
            }
          }
        }

        span {
          font-size: 16px;
          position: relative;
          vertical-align: middle;
          cursor: pointer;
        }
      }
    }

    .nav-plus {
      width: 42px;
      position: relative;
    }
  }
}

.lite-iconf {
  font-size: 28px;
}

.unlogin-logo {
  width: 26px;
  height: 40px;
  background-image: url('../assets/img/pwalogo.svg');
  background-position: 0 12px;
}

.icon-logo-qq,
.unlogin-logo {
  background-repeat: no-repeat;
  background-size: 100%;
}

.m-search {
  display: block;
  background: #e3e4e6;
  color: #939393;
  border-radius: .25rem;
  box-sizing: border-box;
  position: relative;
}

.m-search {
  margin: 8px 9px;
  color: #bdbdbd;
  border-radius: 16px;

  .m-font {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    left: .8125rem;
  }

  .m-font {
    left: 8px;
  }

  .m-text-cut {
    padding-left: 30px !important;
    background-color: #ebebec !important;
    font-size: 14px !important;
  }

  .m-text-cut {
    border: none;
    background: #fff;
    height: 28px;
    line-height: 28px;
    font-size: 14px;
    padding: 0 0 0 30px;
    border-radius: 16px;
  }
}

.m-box-col {
  flex: 1;
  width: 100%;
  display: block;
  min-width: 0;
}

.slide-container {
  height: 44px;

  .slide-wrap {
    position: relative;
    padding-bottom: 12px;
    overflow-x: scroll;
    overflow-y: hidden;
    white-space: nowrap;
  }
}

.m-box-center-a {
  display: flex;
  align-items: center;
}

.m-box-center {
  display: flex;
  justify-content: center;
}

.m-font-arrow-down {
  font-size: 20px;
  transition: transform .4s ease;
}

.lite-nav-sublist {
  position: absolute;
  top: 44px;
  left: 0;
  right: 0;
  text-align: left;
  padding: 0 8px 20px;
  color: #333;
  background-color: #fafafa;

  .m-auto-list {
    width: 100%;
  }

  .m-auto-box {
    position: relative;
    padding: 0 8px;
    font-size: 14px;
    text-align: center;
    margin-top: 8px;

    &.cur {
      color: #ff8200;
    }

    span {
      display: inline-block;
      width: 100%;
      height: 34px;
      line-height: 34px;
      padding: 0 4px;
      vertical-align: middle;
      background-color: #eee;
      border-radius: 3px;
    }
  }
}

.m-auto-box {
  display: inline-block;
  vertical-align: top;
  box-sizing: border-box;
}

.aniRotate {
  transform: rotate(180deg);
}

.m-text-cut {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pannelwrap {
  width: 100%;
  overflow: hidden;
}

.video-player {
  &.mwb-layer {
    background: #000;
    color: #fff;
  }
}

.video-player {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 99999;
}

.mwb-video,
.video-js {
  width: 100%;
  height: 100%;
}
</style>
